#!/bin/bash

. $ZMICRO_HOME/core/env

help() {
  echo "Usage:"
  echo "  zmicro bin <command>"
  echo ""
  echo "Commands:"
  echo "    list | ls   List all available commands"
  echo "    register    Re register all commands"
}

core() {
  if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    help
    exit 0
  fi

  case $1 in
  ls | list)
    print() {
      echo "$1 $(color::success ${@:2})"
    }

    array::each print $(ls $ZMICRO_BIN_PATH)
    ;;
  register)
    # echo "register ZMICRO_PLUGINS_PATH: $ZMICRO_PLUGINS_PATH"
    plugin::register_all $ZMICRO_PLUGINS_PATH
    log::success "register all commands done"
    log::info ""
    zmicro bin list
    ;;
  *)
    log::info "unknown command: $1"
    help
    exit 0
    ;;
  esac
}

run() {
  core $@
}

run $@
